package Service;

import Dao.studentmapper;
import Model.login;
import Model.student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;

public class Crudservice {

    private static InputStream in;
    private static SqlSessionFactory factory;
    private static SqlSession sqlSession;


    public static void init() throws IOException {
        //1.读取加载核心配置文件
        in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        factory = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        sqlSession = factory.openSession();

    }
    public Boolean login(String name, String pwd) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        Boolean aBoolean = mapper.selectone(name, pwd);
//        if(aBoolean) return true;return false;
        return aBoolean;
    }
    public void insert(student o) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        mapper.add(o);
        sqlSession.commit();
    }
    public void delete(Integer id) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        mapper.delete(id);
        sqlSession.commit();
    }
    public ArrayList<student> student() throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        ArrayList<student> select = mapper.select();
        return select;
    }
    public void update(student student) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        mapper.update(student);
        sqlSession.commit();
    }
    public  student selectOneService(Integer id) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        student s = mapper.findById(id);
        return s;
    }
    public  login selectOnelogin(String user) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        login byuser = mapper.findByuser(user);
        return byuser;
    }

    public Boolean same(String name) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        Boolean aBoolean = mapper.same(name);
        return aBoolean;
    }

    public ArrayList<student> pageselect(int start,int page1) throws SQLException, IOException {
        init();
        studentmapper mapper = sqlSession.getMapper(studentmapper.class);
        ArrayList<student> pageselect = mapper.pageselect(start,page1);
        return pageselect;
    }

}
